package net.lab1024.sa.admin.module.business.yct.yctofficial.manager;

import com.google.common.collect.Lists;
import net.lab1024.sa.admin.module.business.category.dao.CategoryDao;
import net.lab1024.sa.admin.module.business.category.domain.entity.CategoryEntity;
import net.lab1024.sa.admin.module.business.category.domain.vo.CategoryTreeVO;
import net.lab1024.sa.admin.module.business.yct.yctofficial.dao.YctOfficialInfoClassDao;
import net.lab1024.sa.admin.module.business.yct.yctofficial.domain.entity.YctOfficialInfoClassEntity;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.lab1024.sa.admin.module.business.yct.yctofficial.domain.form.YctOfficialInfoClassQueryForm;
import net.lab1024.sa.admin.module.business.yct.yctofficial.domain.vo.YctOfficialInfoClassTreeVO;
import net.lab1024.sa.admin.module.business.yct.yctofficial.domain.vo.YctOfficialInfoClassVO;
import net.lab1024.sa.base.common.constant.StringConst;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 易齿通官网类目  Manager
 *
 * @Author zahngben
 * @Date 2024-09-23 10:00:32
 * @Copyright 01
 */
@Service
public class YctOfficialInfoClassManager extends ServiceImpl<YctOfficialInfoClassDao, YctOfficialInfoClassEntity> {
    @Resource
    private YctOfficialInfoClassDao yctOfficialInfoClassDao;

    public List<YctOfficialInfoClassTreeVO> queryYctOfficialInfoClassTree(YctOfficialInfoClassQueryForm queryForms) {
        List<YctOfficialInfoClassVO> allInfoClassList = yctOfficialInfoClassDao.queryYctOfficialInfoClassTree(queryForms);
        List<YctOfficialInfoClassVO> categoryEntityList = allInfoClassList.stream().filter(e -> e.getClassParentId().equals(NumberUtils.LONG_ZERO)).collect(Collectors.toList());

        List<YctOfficialInfoClassTreeVO> treeList = SmartBeanUtil.copyList(categoryEntityList, YctOfficialInfoClassTreeVO.class);
        allInfoClassList.forEach(e -> {
            e.setLabel(e.getName());
            e.setValue(e.getClassId());
            e.setParentId(e.getClassParentId());
        });
        treeList.forEach(e -> {
            e.setLabel(e.getName());
            e.setValue(e.getClassId());
            e.setParentId(e.getClassParentId());
        });
        // 递归设置子类
        this.queryAndSetSubInfoClass(treeList, allInfoClassList);
        return treeList;

    }

    private void queryAndSetSubInfoClass(List<YctOfficialInfoClassTreeVO> treeList, List<YctOfficialInfoClassVO> allInfoClassList) {
        if (CollectionUtils.isEmpty(treeList)) {
            return;
        }
        List<Long> parentIdList = treeList.stream().map(YctOfficialInfoClassTreeVO::getClassId).collect(Collectors.toList());
        List<YctOfficialInfoClassVO> categoryEntityList = allInfoClassList.stream().filter(e -> parentIdList.contains(e.getParentId())).collect(Collectors.toList());
        Map<Long, List<YctOfficialInfoClassVO>> categorySubMap = categoryEntityList.stream().collect(Collectors.groupingBy(YctOfficialInfoClassVO::getParentId));
        treeList.forEach(e -> {
            List<YctOfficialInfoClassVO> childrenEntityList = categorySubMap.getOrDefault(e.getValue(), Lists.newArrayList());
            List<YctOfficialInfoClassTreeVO> childrenVOList = SmartBeanUtil.copyList(childrenEntityList, YctOfficialInfoClassTreeVO.class);
            childrenVOList.forEach(item -> {
                item.setLabel(item.getName());
                item.setValue(item.getClassId());
                item.setParentId(item.getClassParentId());
//                item.setCategoryFullName(e.getCategoryFullName() + StringConst.SEPARATOR_SLASH + item.getCategoryName());
            });
            // 递归查询
            this.queryAndSetSubInfoClass(childrenVOList, allInfoClassList);
            e.setChildren(childrenVOList);
        });
    }
}
